package com.ruoyi.project.im.personhost.controller;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.im.doyuming.domain.Doyuming;
import com.ruoyi.project.im.doyuming.service.IDoyumingService;
import com.ruoyi.project.im.users.domain.Users;
import com.ruoyi.project.im.users.service.IUsersService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.im.personhost.domain.Personhost;
import com.ruoyi.project.im.personhost.service.IPersonhostService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 域名分配列表Controller
 * 
 * @author ruoyi
 * @date 2022-09-11
 */
@Controller
@RequestMapping("/im/personhost")
public class PersonhostController extends BaseController
{
    private String prefix = "im/personhost";

    @Autowired
    private IPersonhostService personhostService;

    @Autowired
    private IUsersService usersService;

    @Autowired
    private IDoyumingService iDoyumingService;

    @RequiresPermissions("im:personhost:view")
    @GetMapping()
    public String personhost()
    {
        return prefix + "/personhost";
    }

    /**
     * 查询域名分配列表列表
     */
    @RequiresPermissions("im:personhost:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Personhost personhost)
    {
        String keyword=personhost.getKeyword();
        if(!StringUtils.isEmpty(keyword)){
            if(keyword.contains(".")){
                personhost.setUrl(keyword);
            }else {
                Users users =usersService.selectUsersByKamicode(keyword);
                if(null!=users){
                    personhost.setUserId(users.getUserId());
                }
            }
        }

        startPage();
        List<Personhost> list = personhostService.selectPersonhostList(personhost);
        return getDataTable(list);
    }

    /**
     * 导出域名分配列表列表
     */
    @RequiresPermissions("im:personhost:export")
    @Log(title = "域名分配列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Personhost personhost)
    {
        List<Personhost> list = personhostService.selectPersonhostList(personhost);
        ExcelUtil<Personhost> util = new ExcelUtil<Personhost>(Personhost.class);
        return util.exportExcel(list, "域名分配列表数据");
    }

    /**
     * 新增域名分配列表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存域名分配列表
     */
    @RequiresPermissions("im:personhost:add")
    @Log(title = "域名分配列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Personhost personhost)
    {
        return toAjax(personhostService.insertPersonhost(personhost));
    }

    /**
     * 修改域名分配列表
     */
    @RequiresPermissions("im:personhost:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Personhost personhost = personhostService.selectPersonhostById(id);
        mmap.put("personhost", personhost);
        return prefix + "/edit";
    }

    /**
     * 修改保存域名分配列表
     */
    @RequiresPermissions("im:personhost:edit")
    @Log(title = "域名分配列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Personhost personhost)
    {
        return toAjax(personhostService.updatePersonhost(personhost));
    }

    /**
     * 删除域名分配列表
     */
    @RequiresPermissions("im:personhost:remove")
    @Log(title = "域名分配列表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String[] id=Convert.toStrArray(ids);
        if (id.length==1){
            Personhost p=personhostService.selectPersonhostById(Long.valueOf(id[0]));
            if(p.getLeixing()==3L){
                Doyuming doyuming=iDoyumingService.selectDoyumingByUrl(p.getLeixing().intValue(),p.getUrl().replace("http://",""));
                if (doyuming==null){
                    return toAjax(false);
                }
                doyuming.setIsUse(0);
                iDoyumingService.updateDoyuming(doyuming);
            }
        }
        return toAjax(personhostService.deletePersonhostByIds(ids));
    }
}
